<script setup>
import { ref , onMounted } from 'vue';
import axios from 'axios';
import {queryAllApi, addDeptApi} from '@/api/dept';
import { ElMessage, ElMessageBox } from 'element-plus';
import { deleteByIdApi, queryByIdApi, updateApi } from '../../api/dept';
// 示例数据
const tableData = ref([]);

// 编辑部门 - 根据ID查询回显数据
const edit = async (row) => {
  formTitle.value = '修改部门'
  showDialog.value = true;
  const res = await queryByIdApi(row.id)
  deptForm.value = res.data
};

// 删除部门 - 根据ID删除部门
const del = (row) => {
  ElMessageBox.confirm(
    '确定要删除此数据吗?',
    '提示',
    {
      confirmButtonText: '确定',
      cancelButtonText: '取消',
      type: 'warning',
    }
  ).then(async () => {
      const res = await deleteByIdApi(row.id)
      if (res.code){
        ElMessage.success('删除成功')
        queryAll()
      }else{
        ElMessage.error(res.msg)
      }
    }).catch(() => {
      ElMessage.info('取消操作')
    })
}


const queryAll = async () => {
  const result = await queryAllApi()
  tableData.value = result.data
}

onMounted(() => {
  queryAll()
});

// 表单标签宽度
const formLabelWidth = ref('120px')
// 表单引用
const deptFormRef = ref(null)

// 重置表单
const resetForm = () => {
  deptFormRef.value.resetFields()
}

const showDialog = ref(false)
const deptForm = ref({name:''})
const formTitle = ref('')
const formRules = ref({
  name: [
    { required: true, message: '请输入部门名称', trigger: 'blur' },
    { min: 2, max: 10, message: '长度在 2 到 10 个字符', trigger: 'blur' }
  ]
})

const add = () => {
  deptForm.value = {name:''}
  formTitle.value = '新增部门';
  showDialog.value = true;
};

const save = async() => {
  await deptFormRef.value.validate (async(valid,fields)=>{
    if (valid){
      let res
      if (deptForm.value.id){
        res = await updateApi(deptForm.value)
      }else{
        res = await addDeptApi(deptForm.value)
      }
      if (res.code){
        ElMessage.success('保存成功')
        showDialog.value = false;
        queryAll();
      }else{
        ElMessage.error('保存失败')
      }
    }else{
      ElMessage.error('校验失败')
    }
  })

  
};


</script>

<template>
  <h1>部门管理</h1>

  <!-- 按钮靠页面右侧显示 -->
  <el-button type="primary" @click="add()" style="float: right;"> + 新增部门</el-button> <br><br>

  <el-table :data="tableData" border style="width: 100%;">
    <el-table-column type="index" label="序号" width="100" align="center"/>
    <el-table-column prop="name" label="部门名称" width="300" align="center"/>
    <el-table-column prop="updateTime" label="最后修改时间" width="300" align="center"/>
    <el-table-column fixed="right" label="操作" align="center">
      <template #default="{ row }">
        <el-button size="small" @click="edit(row)">修改</el-button>
        <el-button size="small" type="danger" @click="del(row)">删除</el-button>
      </template>
    </el-table-column>
  </el-table>
  <el-dialog v-model="showDialog" :title="formTitle" width="30%" @close="resetForm">
    <el-form :model="deptForm" :rules="formRules" ref="deptFormRef">
      <el-form-item label="部门名称" prop="name" label-width="80px">
        <el-input v-model="deptForm.name" autocomplete="off"></el-input>
      </el-form-item>
    </el-form>

    <template #footer>
      <span class="dialog-footer">
        <el-button @click="showDialog = false">取消</el-button>
        <el-button type="primary" @click="save">确定</el-button>
      </span>
    </template>
  </el-dialog>

</template>

<style scoped>

</style>

